package com.itlong.cloud.controller.finance;

import com.itlong.cloud.POJO.DTO.operate.OperateProductTypeManageGetDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateAssignProductModelRelationGetVO;
import com.itlong.cloud.POJO.VO.operate.OperateProductTypeManageGetVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.paymenthub.IOperateProductTypeManageServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-产品类型管理控制器
 * </desc>
 *
 * @createDate 2020/12/03
 */
@RestController
@RequestMapping("/finance/productTypeManage")
@RefreshScope
public class OperateProductTypeManageController extends BaseController {
    private static Logger LOG = LoggerFactory.getLogger(OperateProductTypeManageController.class);
    @Autowired
    private IOperateProductTypeManageServiceClient iOperateProductTypeManageServiceClient;


    /**
     * <desc>
     *      分页获取任务书产品类型信息
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author pengwei
     * @createDate 2020/12/03
     */
    @ResponseBody
    @PostMapping(path = "/getPage")
    public Object  getPage(OperateProductTypeManageGetDTO operateProductTypeManageDTO){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateProductTypeManageDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateProductTypeManageGetDTO.class,request);
            Page<OperateProductTypeManageGetVO> operateProductTypeManageVOPage = iOperateProductTypeManageServiceClient.getPage(operateProductTypeManageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), operateProductTypeManageVOPage);
        } catch (Exception e) {
            throw new DataAccessException("【运营-产品类型管理getPage】获取产品类型列表失败",e);
        }
    }

    /**
     * <desc>
     *      获取产品类型信息详情
     * </desc>
     *
     * @param operateProductTypeManageGetDTO 获取产品类型信息DTO
     * @return 标准格式化响应结果
     * @author pengwei
     * @createDate 2020/12/03
     */
    @ResponseBody
    @PostMapping("getOne")
    public Object getOne(OperateProductTypeManageGetDTO operateProductTypeManageGetDTO){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateProductTypeManageGetDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateProductTypeManageGetDTO.class,request);
            if (operateProductTypeManageGetDTO == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            OperateProductTypeManageGetVO  operateProductTypeManageGetVO = iOperateProductTypeManageServiceClient.getOne(operateProductTypeManageGetDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), operateProductTypeManageGetVO);
        } catch (Exception e) {
            throw new DataAccessException("【运营-产品类型管理getOne】获取产品类型详情失败",e);
        }
    }

    /**
     * <desc>
     *      删除产品类型信息
     * </desc>
     *
     * @param dto 产品类型dto
     * @return 标准格式化响应结果
     * @author pengwei
     * @createDate 2020/12/03
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.FINANCE_PRODUCT_TYPE_DELETE_LOG_DESC)
    @ResponseBody
    @PostMapping("delete")
    public Object delete(OperateProductTypeManageGetDTO dto){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto= HttpProtocolUtil.parseRequestParamToDTO(OperateProductTypeManageGetDTO.class,request);
            if (dto == null || StringUtils.isBlank(dto.getAssignmentProcId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            String ip = HttpProtocolUtil.getIpAddr(request);
            dto.setIp(ip);
            iOperateProductTypeManageServiceClient.delete(dto);
            return new MessageVO(dto.getAssignmentProcId(), BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营-产品类型管理delete】删除产品类型信息失败",e);
        }
    }


    /**
     * <desc>
     * 添加或者修改产品类型
     * <desc/>
     *
     * @param  dto     参数对象
     * @return Object  标准格式化响应结果
     * @author pengwei
     * @createDate 2020/12/03
     */
    @AuthAccessRequired
    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    @LoggerInfo(operateDesc = LogDescConstant.FINANCE_PRODUCT_TYPE_SAVE_OR_UPDATE_LOG_DESC)
    public Object saveOrUpdate(OperateProductTypeManageGetDTO dto) {
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto= HttpProtocolUtil.parseRequestParamToDTO(OperateProductTypeManageGetDTO.class,request);
            if (dto == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            String ip = HttpProtocolUtil.getIpAddr(request);
            dto.setIp(ip);
            MessageVO  resVo  = iOperateProductTypeManageServiceClient.saveOrUpdate(dto);
            return   resVo;
        } catch (Exception e) {
            throw new DataAccessException("【运营-产品类型管理saveOrUpdate】添加或者更新产品类型信息失败",e);
        }
    }



    /**
     * <desc>
     *      分页获取和类型相关联的产品类型列表
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author pengwei
     * @createDate 2020/12/03
     */
    @ResponseBody
    @PostMapping(path = "/relatedProModelList")
    public Object  relatedProModelList(OperateProductTypeManageGetDTO dto){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto= HttpProtocolUtil.parseRequestParamToDTO(OperateProductTypeManageGetDTO.class,request);
            Page<OperateAssignProductModelRelationGetVO> voPage = iOperateProductTypeManageServiceClient.getRelatedProcModelPage(dto);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), voPage);
        } catch (Exception e) {
            throw new DataAccessException("【运营-产品类型管理relatedProcCodeList】获取产品类型关联的产品型号列表失败",e);
        }
    }


    /**
     * <desc>
     *      分页获取产品型号列表
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author pengwei
     * @createDate 2020/12/03
     */
    @ResponseBody
    @PostMapping(path = "/proModelSearch")
    public Object  proModelSearch(OperateProductTypeManageGetDTO dto){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto= HttpProtocolUtil.parseRequestParamToDTO(OperateProductTypeManageGetDTO.class,request);
            Page<OperateAssignProductModelRelationGetVO> voPage = iOperateProductTypeManageServiceClient.getProcModelPage(dto);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), voPage);
        } catch (Exception e) {
            throw new DataAccessException("【运营-产品类型管理proCodeSearch】获取产品型号列表失败",e);
        }
    }

    /**
     * <desc>
     *      获取产品类型下的产品信息列表
     * </desc>
     *
     * @param
     * @return
     * @Author Zhu.yj
     * @createDate  2021/2/22
     */
    @PostMapping("getAssignmentTypeList")
    public Object getAssignmentTypeList(HttpServletRequest request){
        try{
            String productType = request.getAttribute("productType") == null ? null : request.getAttribute("productType").toString();
            if (StringUtils.isBlank(productType)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            List<Map<String, Object>> productInfoList = iOperateProductTypeManageServiceClient.getAssignmentTypeList(productType);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), productInfoList);
        }catch (Exception e){
            throw new DataAccessException("【运营-产品类型管理getAssignmentTypeList】获取产品类型下的产品信息列表失败", e);
        }
    }
}
